package com.ishansong.ticket.service.impl;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import com.beust.jcommander.internal.Maps;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ishansong.ticket.exception.ServiceDaoException;
import com.ishansong.ticket.exception.ServiceException;
import com.ishansong.ticket.model.TicketQaGroup;
import com.ishansong.ticket.service.TicketQaGroupService;
import com.matrixjoy.dal.dao.Dao;
import com.matrixjoy.dal.dao.exception.DaoException;



@Component( value= "ticketQaGroupService" ) 
public class TicketQaGroupServiceImpl implements TicketQaGroupService {

	@Autowired
	private Dao dao;

	@Override
	public Boolean deleteTicketQaGroup(Long id) throws ServiceDaoException, ServiceException {
		try {
			return (Boolean) dao.delete(TicketQaGroup.class, id);
		} catch (DaoException e) {
			throw new ServiceDaoException(e);
		}
	}

	@Override
	public TicketQaGroup getTicketQaGroup(Long id) throws ServiceDaoException, ServiceException {
		try {
			if (null == id) {
				return null;
			}
			return (TicketQaGroup) dao.get(TicketQaGroup.class, id);
		} catch (DaoException e) {
			throw new ServiceDaoException(e);
		}
	}

	@Override
	public Long saveTicketQaGroup(TicketQaGroup ticketQaGroup) throws ServiceDaoException, ServiceException {
		try {
			return (Long) dao.save(ticketQaGroup);
		} catch (DaoException e) {
			throw new ServiceDaoException(e);
		}
	}

	@Override
	public void updateTicketQaGroup(TicketQaGroup ticketQaGroup) throws ServiceDaoException, ServiceException {
		try {
			dao.update(ticketQaGroup);
		} catch (DaoException e) {
			throw new ServiceDaoException(e);
		}

	}

	@Override
	public List<TicketQaGroup> queryTicketQaGroupsByPid(Integer pid,Integer qaType) throws ServiceDaoException, ServiceException {
		try {
			@SuppressWarnings("unchecked")
			List<Serializable> ids = null;
			if(qaType != null) {
				ids = dao.getIdList("queryTicketQaGroupsByPidAndQaType", new Object[] { pid,qaType });
			} else {
				ids = dao.getIdList("queryTicketQaGroupsByPid", new Object[] { pid });
			}

			if (!CollectionUtils.isEmpty(ids)) {
				List<TicketQaGroup> ticketQaGroups = dao.getList(TicketQaGroup.class, ids);
				return ticketQaGroups;
			} else {
				return Collections.emptyList();
			}
		} catch (DaoException e) {
			throw new ServiceDaoException(e);
		}
	}

	@Override
	public Map<Integer, String> queryTicketQaGroups() throws ServiceDaoException, ServiceException {
		try {
			@SuppressWarnings("unchecked")
			List<Serializable> ids = dao.getIdList("queryTicketQaGroups", new Object[] {});
			if (!CollectionUtils.isEmpty(ids)) {
				List<TicketQaGroup> ticketQaGroups = dao.getList(TicketQaGroup.class, ids);
				Map<Integer,String> map = Maps.newHashMap();
				for (TicketQaGroup ticketQaGroup : ticketQaGroups) {
					map.put(ticketQaGroup.getId(),ticketQaGroup.getName());
				}
				return map;
			} else {
				return Collections.emptyMap();
			}
		} catch (DaoException e) {
			throw new ServiceDaoException(e);
		}
	}

	public Dao getDao() {
		return dao;
	}

	public void setDao(Dao dao) {
		this.dao = dao;
	}

}
